﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Police.Rules
{
    [PreCommitRule(RuleName, 1)]
    public sealed class CheckCodeBlockRule : IPreCommitRule
    {
        public const String RuleName = "Check Code Block";


        public void Validate(Configuration.Settings settings, Svn.ISvnChangeInfo changeInfo)
        {
            if (String.IsNullOrEmpty(changeInfo.LogMessage))
                throw new PreCommitRuleException(RuleName, "Comment cannot be empty");

            var comment = changeInfo.LogMessage;
            var pattern = "CodeBlock:";
            var cbIndex = comment.IndexOf(pattern);
            if (cbIndex == -1)
                throw new PreCommitRuleException(RuleName, "CodeBlock prop cannot be empty");

            var cbEnd = comment.Substring(cbIndex).IndexOf('#');
            var cbValue = comment.Substring(pattern.Length, cbEnd);
            using (var log = new StreamWriter("CodeBlocks.log", true))
            {
                log.WriteLine(cbValue);
            }
        }
    }
}
